<?php
namespace Admin\Controller;

use Think\Controller;
use Common\Util\UploadHandler;
use Common\Controller\AdminbaseController;

class IndexController extends AdminbaseController {
	
	public function index() {
		$this->assign("SUBMENU_CONFIG", json_encode(D("Common/Menu")->menu_json()));
    	$this->display();
	}
	
    /**
     * @todo 美滴滴设置
     */
    public function mddConfig() {
    	if (!IS_POST) {
    		$setting = M('config')->where(array('id' => 1))->find();
    		
    		$this->assign('setting', $setting);
	    	$this->display();
    	} else {
    		$postData = I('post.');
    		$id = I('post.id');
    		
    		if ($id) {
    			unset($postData ['id']);
    			$postData ['update_time'] = time();
    			
    			$result = M('config')->where(array('id' => $id))->save($postData);
    			S('GLOBLE_SETTING', null);
    		} else {
    			$postData ['id'] = 1;
    			$result = M('config')->add($postData);
    		}
    		
    		if ($result) {
	    		$this->success('设置成功');
    		} else {
    			$this->success('设置失败');
    		}
    	}
    }
    
    public function addHomeCarousel() {
    	if (IS_POST) {
    		$PhotoModel = D('Photo');
    		
    		$photos = $PhotoModel->findHomeCarousel();
    		if ($photos) {
    			$sort = $PhotoModel->getSortByTarget(0);
    		} else {
    			$sort = 0;
    		}
    		
    		$carousels = I('post.cover_img_name');
    		$uploadNumber = I('post.number');
    		$time = time();
    		
    		$photoData = array(
    			'type' => 'h_carousel',
    			'target_id' => 0,
    			'created'	=> $time,
    			'upload_number' => $uploadNumber
    		);
    		foreach ($carousels as $key => $value) {
    			++$sort;
    			$photoData ['sort'] = $sort;
    			$photoData ['img_name'] = $value;
    			$addResult = $PhotoModel->add($photoData);
    			if (!$addResult) {
    				$this->error('添加失败');
    			}
    		}
    		
    		$PhotoModel->clearHomeCarouselCache();
    		$this->success('添加成功', U('homeCarousel'));
    	} else {
    		$this->assign('number', md5(microtime()));
    		$this->display();
    	}
    }
    
    public function ban() {
    	$id = I('get.id');
    	 
    	$PhotoModel = D('Photo');
    	$result = $PhotoModel->where(array('id' => $id))->save(array('status' => 2));
    	if (!$result) {
    		$this->success('修改失败');
    	}
    	 
    	$PhotoModel->clearHomeCarouselCache();
    	$this->success('修改成功');
    }
    
    public function cancelBan() {
    	$id = I('get.id');
    	
    	$PhotoModel = D('Photo');
    	$result = $PhotoModel->where(array('id' => $id))->save(array('status' => 1));
    	if (!$result) {
    		$this->success('修改失败');
    	}
    	
    	$PhotoModel->clearHomeCarouselCache();
    	$this->success('修改成功');
    }
    
    public function deleteCarousel() {
    	$id = I('get.id');
    	
    	$PhotoModel = D('Photo');
    	$result = $PhotoModel->where(array('id' => $id))->save(array('status' => 0));
    	if (!$result) {
    		$this->error('删除失败');
    		exit();
    	}
    	
    	$PhotoModel->clearHomeCarouselCache();
    	$this->success('删除成功');
    }
    
    public function updateSort() {
    	$ids = I('post.id');
    	
    	$sorts = I ('post.sort');
    	
    	$PhotoModel = D('Photo');
    	$photos = $PhotoModel->findHomeCarousel();
    	foreach ($photos as $key => $value) {
    		if (($index = array_search($value['id'], $ids))) {
	    		if ($value ['sort'] != $sorts[$index]) {
	    			$PhotoModel->where(array('id' => $value ['id']))->save(array('sort' => $sorts[$index]));
	    		}
    		}
    	}
    	
    	$PhotoModel->clearHomeCarouselCache();
    	$this->success('排序已更新');
    }
    
    public function homeCarousel() {
    	$PhotoModel = D('Photo');
    	$photos = $PhotoModel->where(array('status' => array('neq', 0), 'type'=> 'h_carousel'))
    		->order('sort')->select();
    		
    	$this->assign('carousels', $photos);
    	$this->display();
    }
    
    public function uploadCarousel() {
    	$options = C('HOME_CAROUSEL_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Home/Carousel/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name]);
    }
}